import Vue from 'vue'
import Vuex from 'vuex'
//引入token本地操作
import { setToken, getToken, removeToken } from '@/utils/token.js';
//引入接口请求函数
import { reqUserLogin, reqUserRegister, reqBanner, reqTypeOne, reqAllGoodsList, reqTypeTwoList, reqTypeTwoDataList, reqSearchList, reqDetailById, reqSameList, reqAddShopCard, reqShopList, reqChangeCount, reqDeleteShopCard } from '@/api';
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo: JSON.parse(getToken()) || {},//用户信息，里面有token
    typeOneList: [], //一级分类
    bannerList: [], //轮阔图数据
    allGoodsList: [],//所有商品的列表数据
    typeTwoList: [],//二级列表
    typeTwoDataList: [],//二级列表商品
    searchList: [],//通关关键字检索得到的数据
    detailInfo: {},//根据id获取详情的数据
    sameList: [],//同店商品推荐列表
    shopList: [],//购物车列表
  },
  getters: {
  },
  mutations: {
    //登录
    USERLOGIN(state, userInfo) {
      state.userInfo = userInfo
    },
    //退出登录
    LOGOUT(state) {
      state.userInfo = {}
    },
    //获取一级分类
    GETTYPEONE(state, typeOneList) {
      state.typeOneList = typeOneList
    },
    //获取轮阔图数据
    GETBANNER(state, bannerList) {
      state.bannerList = bannerList
    },
    //获取所有商品数据
    GETALLGOODSLIST(state, allGoodsList) {
      state.allGoodsList = allGoodsList
    },
    //根据二级列表
    GETTYPETWOLIST(state, typeTwoList) {
      state.typeTwoList = typeTwoList
      state.typeTwoDataList = []
    },
    // 获取二级商品列表
    GETTYPETWODAIALIST(state, res) {
      state.typeTwoDataList.push(res)
    },
    //根据word检索商品数据
    GETGOODSEARCHBYWORD(state, searchList) {
      state.searchList = searchList
    },
    //根据id获取详情
    GETDETAILBYID(state, detailInfo) {
      state.detailInfo = detailInfo
    },
    //获取同店商品推荐列表
    GETSAMELIST(state, sameList) {
      state.sameList = sameList
    },
    //获取购物车列表
    GETSHOPLIST(state, shopList) {
      state.shopList = shopList
    },
    //点击全选按钮，改变所有商品状态
    CHANGEALL(state, status) {
      state.shopList.forEach(item => item.status = status)
    }
  },
  actions: {
    //登录，并持久化存储
    async userLogin({ commit }, loginForm) {
      const { data: res } = await reqUserLogin(loginForm)
      if (res.code !== 1) return 0
      commit('USERLOGIN', res)
      setToken(JSON.stringify(res))
      return 1
    },
    //退出登录
    logout({ commit }) {
      removeToken()
      commit('LOGOUT')
    },
    //注册
    async userRegister({ commit }, registerForm) {
      const { data: res } = await reqUserRegister(registerForm)
      return res.code
    },
    //获取一级分类
    async getTypeOne({ commit }) {
      const { data: res } = await reqTypeOne()
      commit('GETTYPEONE', res.slice(0, 9))
      return res.lenght
    },
    //获取轮阔图数据
    async getBanner({ commit }) {
      const { data: res } = await reqBanner()
      if (res.code === 1)
        commit('GETBANNER', res.data)
      return res.code
    },
    //获取所有商品数据
    async getAllGoodsList({ commit }) {
      const { data: res } = await reqAllGoodsList()
      commit('GETALLGOODSLIST', res)
      return res.lenght
    },
    //获取二级列表
    async getTypeTwoList({ commit }, word) {
      const { data: res } = await reqTypeTwoList(word)
      commit('GETTYPETWOLIST', res)
      return res
    },
    // 获取二级商品列表
    async getTypeTwoDataList({ commit }, { type_one, type_two, index }) {
      const { data: res } = await reqTypeTwoDataList(type_one, type_two)
      commit('GETTYPETWODAIALIST', res)
    },
    //根据word检索商品数据
    async getGoodSearchByWord({ commit }, word) {
      const { data: res } = await reqSearchList(word)
      if (res.lenght !== 0) {
        commit('GETGOODSEARCHBYWORD', res)
        return 1
      }
      else return 0
    },
    //根据id获取详情
    async getDetailById({ commit }, id) {
      const { data: res } = await reqDetailById(id)
      if (res.lenght == 0) return 0
      commit('GETDETAILBYID', res[0])
      return 1
    },
    //获取同店商品推荐列表
    async getSameList({ commit }, supplier) {
      const { data: res } = await reqSameList(supplier)
      commit('GETSAMELIST', res.slice(0, 5))
    },
    //添加购物车
    async addShopCard({ state }, id) {
      const { data: res } = await reqAddShopCard(state.userInfo.token, id)
      return res
    },
    //获取购物车列表
    async getShopList({ commit, state }) {
      const { data: res } = await reqShopList(state.userInfo.token)
      res.forEach(item => item.status = false)
      commit('GETSHOPLIST', res)
    },
    //改变商品的个数
    async changeCount({ state }, { id, count }) {
      const { data: res } = await reqChangeCount(state.userInfo.token, id, count)
      return res
    },
    //删除购物车
    async deleteShopCard({ state }, id) {
      const { data: res } = await reqDeleteShopCard(state.userInfo.token, id)
      return res
    }
  },
  modules: {
  }
})
